﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using WarehouseManagement.BusinessLogic.BC;
using WarehouseManagement.BusinessLogic.DTO;
using WarehouseManagement.Shared;

namespace WarehouseManagement.Views.Shared
{
    public partial class Receipt : DevExpress.XtraEditors.XtraForm
    {
        private ReceiptBC _receiptBC;
        public delegate void ReceiptCreatedHandler(bool success);
        public ReceiptCreatedHandler OnReceiptCreated;

        public Receipt()
        {
            InitializeComponent();
            _receiptBC = new ReceiptBC();
        }

        public int PartnerId
        {
            get { return Convert.ToInt32(txtPartner.Tag); }
            set { txtPartner.Tag = value; }
        }

        public string PartnerName
        {
            get
            {
                return txtPartner.Text;
            }
            set { txtPartner.Text = value; }
        }
        public string ReceiptNumber
        {
            get { return txtReceiptNumber.Text; }
            set { txtReceiptNumber.Text = value; }
        }
        public string DocumentNumber
        {
            get { return txtDocumentNumber.Text; }
            set { txtDocumentNumber.Text = value; }
        }
        public DateTime DocumentDate
        {
            get { return txtDocumentDate.DateTime; }
            set { txtDocumentDate.DateTime = value; }
        }
        public DateTime ReceiptDate
        {
            get { return txtReceiptDate.DateTime; }
            set { txtReceiptDate.DateTime = value; }
        }
        public decimal Debt
        {
            get { return txtDebt.Value; }
            set { txtDebt.Value = value; }
        }
        public decimal RemainDebt
        {
            get { return txtRemainDebt.Value; }
            set { txtRemainDebt.Value = value; }
        }
        public decimal Paid
        {
            get { return txtPaid.Value; }
            set { txtPaid.Value = value; }
        }
        public string Reason
        {
            get { return txtReason.Text; }
            set { txtReason.Text = value; }
        }

        private void Receipt_Load(object sender, EventArgs e)
        {
            ReceiptNumber = _receiptBC.GenerateReceiptNumber();

        }


        private bool Validate()
        {
            if (Paid > RemainDebt || Paid <= 0)
            {
                XtraMessageBox.Show("Bạn hãy kiểm tra lại số tiền", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Paid = RemainDebt;
                return false;
            }
            return true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!Validate()) return;
            bool ok = true;
            var dto = GetReceiptDTO();
            try
            {
                _receiptBC.AddReceipt(dto);

            }
            catch
            {
                ok = false;
                XtraMessageBox.Show("Đã xảy ra lỗi khi tạo phiếu thu. Xin vui lòng thử lại sau.", "Không thể tạo phiếu thu", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (OnReceiptCreated != null)
                {
                    OnReceiptCreated(ok);
                }
                this.Close();
            }
        }

        private ReceiptDTO GetReceiptDTO()
        {
            return new ReceiptDTO()
            {
                CreatedBy = GlobalMemory.CurrentLoginUser,
                CreatedDate = DateTime.Now,
                PartnerName = PartnerName,
                Paid = Paid,
                Debt = Debt,
                DocumentDate = DocumentDate,
                DocumentNumber = DocumentNumber,
                Id = Guid.NewGuid(),
                PartnerId = PartnerId,
                Reason = Reason,
                ReceiptDate = ReceiptDate,
                ReceiptNumber = ReceiptNumber,
                RemainDebt = RemainDebt
            };
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}